package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.*;
import com.prd.enums.CustomerContactEnum;
import com.prd.enums.CustomerRemarkEnum;
import com.prd.mapper.CustomerGuestMapper;
import com.prd.model.CustomerCommRecordDO;
import com.prd.model.CustomerContactDO;
import com.prd.model.CustomerGuestDO;
import com.prd.model.CustomerRemarkDO;
import com.prd.service.*;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.tools.DTOUtil;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户-客人总表 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-11-27
 */
@Service("CustomerGuestService")
public class CustomerGuestServiceImpl extends ServiceImpl<CustomerGuestMapper, CustomerGuestDO> implements CustomerGuestService {

    @Autowired
    private CustomerGuestMapper guestMapper;

    @Resource(name = "CustomerService")
    private CustomerService customerService;
    @Resource(name = "CustomerContactService")
    private CustomerContactService customerContactService;
    @Resource(name = "CustomerCommRecordService")
    private CustomerCommRecordService recordService;
    @Resource(name = "CustomerRemarkService")
    private CustomerRemarkService remarkService;
    @Resource(name = "CustomerContactService")
    private CustomerContactService contactService;
    @Resource(name = "CustomerProjectService")
    private CustomerProjectService projectService;
    @Resource(name = "CustomerGuestPersonalDetailService")
    private CustomerGuestPersonalDetailService personalDetailService;
    @Resource(name = "CustomerGuestFamilyDetailService")
    private CustomerGuestFamilyDetailService familyDetail;
    @Resource(name = "CustomerGuestPotentialDetailService")
    private CustomerGuestPotentialDetailService potentialDetailService;

    @Override
    public CustomerGuestDO getOne(String keyWord) {
        if (keyWord != null) {
            LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, keyWord)
                    .or()
                    .like(CustomerGuestDO::getGuestName, keyWord)
                    .or()
                    .like(CustomerGuestDO::getGuestPhone, keyWord);
            return guestMapper.selectOne(lambdaQueryWrapper);
        }
        return null;
    }

    @Override
    public void removeOneByCustomerNo(String customerNo) {
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getCustomerNo, customerNo);
        guestMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public void removeOneByGuestNo(String guestNo) {
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        guestMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public CustomerGuestDTO getOneByGuestNo(String guestNo) {
        if (guestNo != null) {
            LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
            return doToDto(guestMapper.selectOne(lambdaQueryWrapper));
        }
        return null;
    }

    @Override
    public JsonData uAddSimple(CustomerGuestDTO guestDTO) {
        String guestNo = String.valueOf(guestDTO.hashCode());
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDO = guestMapper.selectOne(lambdaQueryWrapper);
        if (guestDO == null) {
            guestDO = new CustomerGuestDO();
            guestDTO.setGuestNo(guestNo);
            BeanUtils.copyProperties(guestDTO, guestDO);
            guestDO.setCreateTime(new Date());
            guestMapper.insert(guestDO);

            //客人需求
            if (guestDTO.getGuestRequireList() != null && !guestDTO.getGuestRequireList().isEmpty()) {
                for (CustomerRemarkDTO requireDto: guestDTO.getGuestRequireList()) {
                    requireDto.setType(CustomerRemarkEnum.CustomerRequirementRemark.getCode());
                    requireDto.setNo(guestNo);
                    remarkService.uAdd(requireDto);
                }
            }
            //客人分析
            if (guestDTO.getGuestAnalyzeList() != null && !guestDTO.getGuestAnalyzeList().isEmpty()) {
                for (CustomerRemarkDTO requireDto: guestDTO.getGuestAnalyzeList()) {
                    requireDto.setType(CustomerRemarkEnum.CustomerAnalyze.getCode());
                    requireDto.setNo(guestNo);
                    remarkService.uAdd(requireDto);
                }
            }
            //客人备注
            if (guestDTO.getGuestRemarkList() != null && !guestDTO.getGuestRemarkList().isEmpty()) {
                for (CustomerRemarkDTO remarkDto: guestDTO.getGuestRemarkList()) {
                    remarkDto.setType(CustomerRemarkEnum.CustomerRemark.getCode());
                    remarkDto.setNo(guestNo);
                    remarkService.uAdd(remarkDto);
                }
            }
            //客人来源
            if (guestDTO.getGuestSourceList() != null && !guestDTO.getGuestSourceList().isEmpty()) {
                for (CustomerContactDTO customerSource: guestDTO.getGuestSourceList()) {
                    customerSource.setNo(guestNo);
                    customerSource.setType(CustomerContactEnum.CustomerSource.getCode());
                    contactService.uAdd(customerSource);
                }
            }
            //客人关系人
            if (guestDTO.getGuestRelevantList() != null && !guestDTO.getGuestRelevantList().isEmpty()) {
                for (CustomerContactDTO customerRelevant: guestDTO.getGuestRelevantList()) {
                    customerRelevant.setNo(guestNo);
                    customerRelevant.setType(CustomerContactEnum.CustomerRelevant.getCode());
                    contactService.uAdd(customerRelevant);
                }
            }
            //来往沟通记录
            if (guestDTO.getGuestCommRecordList() != null && !guestDTO.getGuestCommRecordList().isEmpty()) {
                for (CustomerCommRecordDTO customerRecord: guestDTO.getGuestCommRecordList()) {
                    customerRecord.setNo(guestNo);
                    recordService.uAdd(customerRecord);
                }
            }

            return JsonData.buildSuccess(guestDO);
        }
        else {
            return new JsonData(-1, guestDO, "已经有报备");
        }
    }

    @Override
    public JsonData addSimple(CustomerGuestDO guestDO) {
        String guestNo = String.valueOf(guestDO.hashCode());
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDB = guestMapper.selectOne(lambdaQueryWrapper);
        if (guestDB == null) {
            guestDB = new CustomerGuestDO();
            guestDO.setGuestNo(guestNo);
            BeanUtils.copyProperties(guestDO, guestDB);
            guestDB.setCreateTime(new Date());
            guestDB.setIsSimple(2);
            guestMapper.insert(guestDB);
            return JsonData.buildSuccess(guestDO);
        }
        else {
            if (guestDB.getCustomerNo() != null)
                return new JsonData(-1, guestDO, "已经有报备");
            else {
                return update(guestDO);
            }
        }
    }

    @Override
    public JsonData uAddDetail(CustomerGuestDTO guestDTO) {
        String guestNo = String.valueOf(guestDTO.hashCode());
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDO = guestMapper.selectOne(lambdaQueryWrapper);
        if (guestDO == null) {
            guestDO = new CustomerGuestDO();
            guestDTO.setGuestNo(guestNo);
            BeanUtils.copyProperties(guestDTO, guestDO);
            guestDO.setCreateTime(new Date());
            guestDO.setIsSimple(2);
            guestMapper.insert(guestDO);

            //客人需求
            if (guestDTO.getGuestRequireList() != null && !guestDTO.getGuestRequireList().isEmpty()) {
                for (CustomerRemarkDTO requireDto: guestDTO.getGuestRequireList()) {
                    requireDto.setType(CustomerRemarkEnum.CustomerRequirementRemark.getCode());
                    requireDto.setNo(guestNo);
                    remarkService.uAdd(requireDto);
                }
            }
            //客人分析
            if (guestDTO.getGuestAnalyzeList() != null && !guestDTO.getGuestAnalyzeList().isEmpty()) {
                for (CustomerRemarkDTO requireDto: guestDTO.getGuestAnalyzeList()) {
                    requireDto.setType(CustomerRemarkEnum.CustomerAnalyze.getCode());
                    requireDto.setNo(guestNo);
                    remarkService.uAdd(requireDto);
                }
            }
            //客人备注
            if (guestDTO.getGuestRemarkList() != null && !guestDTO.getGuestRemarkList().isEmpty()) {
                for (CustomerRemarkDTO remarkDto: guestDTO.getGuestRemarkList()) {
                    remarkDto.setType(CustomerRemarkEnum.CustomerRemark.getCode());
                    remarkDto.setNo(guestNo);
                    remarkService.uAdd(remarkDto);
                }
            }
            //客人来源
            if (guestDTO.getGuestSourceList() != null && !guestDTO.getGuestSourceList().isEmpty()) {
                for (CustomerContactDTO customerSource: guestDTO.getGuestSourceList()) {
                    customerSource.setNo(guestNo);
                    customerSource.setType(CustomerContactEnum.CustomerSource.getCode());
                    contactService.uAdd(customerSource);
                }
            }
            //客人关系人
            if (guestDTO.getGuestRelevantList() != null && !guestDTO.getGuestRelevantList().isEmpty()) {
                for (CustomerContactDTO customerRelevant: guestDTO.getGuestRelevantList()) {
                    customerRelevant.setNo(guestNo);
                    customerRelevant.setType(CustomerContactEnum.CustomerRelevant.getCode());
                    contactService.uAdd(customerRelevant);
                }
            }
            //来往沟通记录
            if (guestDTO.getGuestCommRecordList() != null && !guestDTO.getGuestCommRecordList().isEmpty()) {
                for (CustomerCommRecordDTO customerRecord: guestDTO.getGuestCommRecordList()) {
                    customerRecord.setNo(guestNo);
                    recordService.uAdd(customerRecord);
                }
            }
            return JsonData.buildSuccess(guestDTO);
        }
        else
            return new JsonData(-1, guestDO, "已经有报备");
    }

    @Override
    public JsonData uUpdateAndAdd(CustomerGuestDTO guestDTO) {
        String guestNo = guestDTO.getGuestNo();
        if (guestNo == null)
            return JsonData.buildError("guestNo参数为空");

        List<CustomerRemarkDO> guestRequireInDBList = remarkService.uSearchForList(guestNo, CustomerRemarkEnum.CustomerRequirementRemark.getCode())
                .stream().map(DTOUtil::remarkDtoToDO).collect(Collectors.toList());
        List<CustomerRemarkDO> guestAnalyzeInDBList = remarkService.uSearchForList(guestNo, CustomerRemarkEnum.CustomerAnalyze.getCode())
                .stream().map(DTOUtil::remarkDtoToDO).collect(Collectors.toList());
        List<CustomerRemarkDO> guestRemarkInDBList = remarkService.uSearchForList(guestNo, CustomerRemarkEnum.CustomerRemark.getCode())
                .stream().map(DTOUtil::remarkDtoToDO).collect(Collectors.toList());
        List<CustomerContactDO> guestContactInDBList = contactService.uSearchForList(guestNo, CustomerContactEnum.CustomerSource.getCode())
                .stream().map(DTOUtil::contactDtoToDO).collect(Collectors.toList());
        List<CustomerContactDO> guestRelevantInDBList = contactService.uSearchForList(guestNo, CustomerContactEnum.CustomerRelevant.getCode())
                .stream().map(DTOUtil::contactDtoToDO).collect(Collectors.toList());
        List<CustomerCommRecordDO> guestCommRecordInDBList = recordService.uSearchForList(guestNo)
                .stream().map(DTOUtil::commRecordDtoToDO).collect(Collectors.toList());

        HashMap<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("guestNo", guestNo);

        //客人需求
        otherParam.put("code", CustomerRemarkEnum.CustomerRequirementRemark.getCode());
        DTOUtil.compareDBAndDOList(guestRequireInDBList,
                guestDTO.getGuestRequireList().stream().map(DTOUtil::remarkDtoToDO).collect(Collectors.toList()),
                remarkService, otherParam);

        //客人分析
        otherParam.put("code", CustomerRemarkEnum.CustomerAnalyze.getCode());
        DTOUtil.compareDBAndDOList(guestAnalyzeInDBList,
                guestDTO.getGuestAnalyzeList().stream().map(DTOUtil::remarkDtoToDO).collect(Collectors.toList()),
                remarkService, otherParam);

        //客人备注
        otherParam.put("code", CustomerRemarkEnum.CustomerRemark.getCode());
        DTOUtil.compareDBAndDOList(guestRemarkInDBList,
                guestDTO.getGuestRemarkList().stream().map(DTOUtil::remarkDtoToDO).collect(Collectors.toList()),
                remarkService, otherParam);

        //客人来源
        otherParam.put("code", CustomerContactEnum.CustomerSource.getCode());
        DTOUtil.compareDBAndDOList(guestContactInDBList,
                guestDTO.getGuestSourceList().stream().map(DTOUtil::contactDtoToDO).collect(Collectors.toList()),
                contactService, otherParam);

        //客人关系人
        otherParam.put("code", CustomerContactEnum.CustomerRelevant.getCode());
        DTOUtil.compareDBAndDOList(guestRelevantInDBList,
                guestDTO.getGuestRelevantList().stream().map(DTOUtil::contactDtoToDO).collect(Collectors.toList()),
                contactService, otherParam);

        //来往沟通记录
        DTOUtil.compareDBAndDOList(guestCommRecordInDBList,
                guestDTO.getGuestCommRecordList().stream().map(DTOUtil::commRecordDtoToDO).collect(Collectors.toList()),
                recordService, otherParam);

        return update(DTOUtil.guestDtoToDO(guestDTO));
    }

    @Override
    public JsonData update(CustomerGuestDO guestDO) {
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestDO.getGuestNo());
        CustomerGuestDO guestDB = guestMapper.selectOne(lambdaQueryWrapper);
        if (guestDB == null) {
            return JsonData.buildError("未找到对应客人");
        } else {
            CopyNonNullBeanUtils.copyProperties(guestDO, guestDB);
            guestDB.setIsSimple(2);
            guestMapper.update(guestDB, lambdaQueryWrapper);
            return JsonData.buildSuccess(guestDB);
        }
    }

    @Override
    public JsonData uMerge(CustomerGuestDTO guestDTO) {
        String guestNo = String.valueOf(guestDTO.hashCode());
        LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDO = guestMapper.selectOne(lambdaQueryWrapper);
        if (guestDO == null)
            return JsonData.buildError("未找到可合并客人，请确认是否已经报备后再进行合并");
        CopyNonNullBeanUtils.copyProperties(guestDTO, guestDO);
        guestDO.setIsSimple(2);
        guestMapper.update(guestDO, lambdaQueryWrapper);
        //客人来源
        if (guestDTO.getGuestSourceList() != null && !guestDTO.getGuestSourceList().isEmpty()) {
            for (CustomerContactDTO customerSource: guestDTO.getGuestSourceList()) {
                customerSource.setNo(guestNo);
                customerSource.setType(CustomerContactEnum.CustomerSource.getCode());
                customerContactService.uUpdate(customerSource);
            }
        }

        //客人关系人
        if (guestDTO.getGuestRelevantList() != null && !guestDTO.getGuestRelevantList().isEmpty()) {
            for (CustomerContactDTO customerRelevant: guestDTO.getGuestRelevantList()) {
                customerRelevant.setNo(guestNo);
                customerRelevant.setType(CustomerContactEnum.CustomerRelevant.getCode());
                customerContactService.uUpdate(customerRelevant);
            }
        }
        return JsonData.buildSuccess(guestDO);
    }

    @Override
    public List<CustomerGuestDTO> searchForList(String customerNo) {
        List<CustomerGuestDTO> guestDTOList = new ArrayList<>();
        if (customerNo != null) {
            LambdaQueryWrapper<CustomerGuestDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(CustomerGuestDO::getCustomerNo, customerNo);
            List<CustomerGuestDO> customerGuestDOList = guestMapper.selectList(lambdaQueryWrapper);
            for (CustomerGuestDO guestDO: customerGuestDOList) {
                CustomerGuestDTO guestDTO = new CustomerGuestDTO();
                BeanUtils.copyProperties(guestDO, guestDTO);
                guestDTOList.add(guestDTO);
            }
        }
        return guestDTOList;
    }


    @Override
    public JsonData uInfo(String guestNo) {
        if (guestNo == null)
            return JsonData.buildError("guestNo参数为空");
        LambdaQueryWrapper<CustomerGuestDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDO = guestMapper.selectOne(wrapper);
        CustomerGuestDTO guestDTO = new CustomerGuestDTO();
        if (guestDO == null) {
            return JsonData.buildError("查无此客人");
        }
        BeanUtils.copyProperties(guestDO, guestDTO);
        //如果已经绑定了客户，则查询客户的项目
        if (guestDO.getCustomerNo() != null) {
            List<CustomerProjectDTO> projectDTOList = projectService.searchForList(guestDO.getCustomerNo());
            guestDTO.setProjectList(projectDTOList);
        }
        return JsonData.buildSuccess(guestDTO);
    }

    @Override
    public JsonData uRemoveCustomer(String customerNo, String guestNo) {
        if (customerNo == null || guestNo == null)
            return JsonData.buildError("customerNo/guestNo参数为空");
        LambdaQueryWrapper<CustomerGuestDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDO = guestMapper.selectOne(wrapper);
        if (guestDO == null) {
            return JsonData.buildError("查无此客人");
        }
        if (guestDO.getCustomerNo() == null) {
            return JsonData.buildError("此客人查无客户信息");
        }
        guestDO.setCustomerNo(null);
        customerService.uRemoveGuestInfo(customerNo, guestNo);
        UpdateWrapper<CustomerGuestDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(CustomerGuestDO::getGuestNo, guestDO.getGuestNo())
                .set(CustomerGuestDO::getCustomerNo, null);
        guestMapper.update(guestDO, updateWrapper);
        return JsonData.buildSuccess(guestDO);
    }

    @Override
    public JsonData uAddAndUpdateDetail(CustomerGuestDTO guestDTO) {
        if (guestDTO.getGuestNo() == null)
            return JsonData.buildError("guestNo参数为空");
        personalDetailService.uUpdateAndAdd(guestDTO.getPersonalDetail());
        potentialDetailService.uUpdateAndAdd(guestDTO.getPotentialDetail());
        familyDetail.uUpdateAndAdd(guestDTO.getFamilyDetail());
        guestDTO.setPersonalDetail(personalDetailService.search(guestDTO.getGuestNo()));
        guestDTO.setPotentialDetail(potentialDetailService.search(guestDTO.getGuestNo()));
        guestDTO.setFamilyDetail(familyDetail.search(guestDTO.getGuestNo()));
        return JsonData.buildSuccess(guestDTO);
    }

    @Override
    public JsonData uGetDetailInfo(String guestNo) {
        if (guestNo == null)
            return JsonData.buildError("guestNo参数为空");
        CustomerGuestDTO guestDTO = new CustomerGuestDTO();
        guestDTO.setGuestNo(guestNo);
        guestDTO.setPersonalDetail(personalDetailService.search(guestDTO.getGuestNo()));
        guestDTO.setPotentialDetail(potentialDetailService.search(guestDTO.getGuestNo()));
        guestDTO.setFamilyDetail(familyDetail.search(guestDTO.getGuestNo()));
        return JsonData.buildSuccess(guestDTO);
    }

    @Override
    public JsonData uInfo2(String guestNo) {
        LambdaQueryWrapper<CustomerGuestDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerGuestDO::getGuestNo, guestNo);
        CustomerGuestDO guestDO = guestMapper.selectOne(wrapper);
        CustomerGuestDTO guestDTO = new CustomerGuestDTO();
        if (guestDO == null) {
            return JsonData.buildError("查无此客人");
        }
        BeanUtils.copyProperties(guestDO, guestDTO);
        //如果已经绑定了客户，则查询客户的项目
        if (guestDO.getCustomerNo() != null) {
            List<CustomerProjectDTO> projectDTOList = projectService.searchForList(guestDO.getCustomerNo());
            guestDTO.setProjectList(projectDTOList);
        }
        //客人需求
        guestDTO.setGuestRequireList(
                remarkService.uSearchForList(guestNo, CustomerRemarkEnum.CustomerRequirementRemark.getCode())
        );
        //客人分析
        guestDTO.setGuestAnalyzeList(
                remarkService.uSearchForList(guestNo, CustomerRemarkEnum.CustomerAnalyze.getCode())
        );
        //客人备注
        guestDTO.setGuestRemarkList(
                remarkService.uSearchForList(guestNo, CustomerRemarkEnum.CustomerRemark.getCode())
        );
        //客人来源
        guestDTO.setGuestSourceList(
                contactService.uSearchForList(guestNo, CustomerContactEnum.CustomerSource.getCode())
        );
        //客人关系人
        guestDTO.setGuestRelevantList(
                contactService.uSearchForList(guestNo, CustomerContactEnum.CustomerRelevant.getCode())
        );
        //来往沟通记录
        guestDTO.setGuestCommRecordList(
                recordService.uSearchForList(guestNo)
        );
        return JsonData.buildSuccess(guestDTO);
    }

    @Override
    public Map<String, Object> uQueryGuest(int page, int size, String keyWord, int customerType, int customerStatus, int softOf, int orderBy, String address, int guestLevel, int businessStatus, int lastBusinessDay) {
        Page<CustomerGuestDO> pageInfo = new Page<>(page, size);
        IPage<CustomerGuestDO> guestDOIPage = guestMapper.selectPage(pageInfo,
                createSearchWrapper(keyWord, softOf, orderBy, address, guestLevel, businessStatus, lastBusinessDay));
        List<CustomerGuestDTO> guestDTOList = guestDOIPage.getRecords().stream().map(
                this::doToDto
        ).collect(Collectors.toList());
        for (CustomerGuestDTO guestDTO: guestDTOList) {
            if (guestDTO.getCustomerNo() != null) {
                guestDTO.setProjectList(projectService.searchForList(guestDTO.getCustomerNo()));
                guestDTO.setCustomer(customerService.getOne(guestDTO.getCustomerNo()));
            }
        }
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_record", guestDOIPage.getTotal());
        map.put("total_page", guestDOIPage.getPages());
        map.put("current_data", guestDTOList);
        return map;
    }

    @Override
    public List<CustomerGuestDTO> uQueryGuest2(String keyWord, int customerType, int customerStatus, int softOf, int orderBy, String address, int guestLevel,
                                               int businessStatus, int lastBusinessDay) {
        List<CustomerGuestDO> guestDOList = guestMapper.selectList(
                createSearchWrapper(keyWord, softOf, orderBy, address, guestLevel, businessStatus, lastBusinessDay));
        List<CustomerGuestDTO> guestDTOList = guestDOList.stream().map(this::doToDto).collect(Collectors.toList());
        for (CustomerGuestDTO guestDTO: guestDTOList) {
            if (guestDTO.getCustomerNo() != null) {
                guestDTO.setProjectList(projectService.searchForList(guestDTO.getCustomerNo()));
                guestDTO.setCustomer(customerService.getOne(guestDTO.getCustomerNo()));
            }
        }
        return guestDTOList;
    }


    private Wrapper<CustomerGuestDO> createSearchWrapper(String keyWord, int softOf, int orderBy, String address, int guestLevel, int businessStatus, int lastBusinessDay) {
        LambdaQueryWrapper<CustomerGuestDO> wrapper = new LambdaQueryWrapper<CustomerGuestDO>();
        if (keyWord != null) {
            wrapper.like(CustomerGuestDO::getGuestName, keyWord);
            wrapper.or().like(CustomerGuestDO::getCustomerNo, keyWord);
            wrapper.or().like(CustomerGuestDO::getGuestPhone, keyWord);
        }

        if (address != null)
            wrapper.like(CustomerGuestDO::getGuestAddress, address);
        if (guestLevel != -1)
            wrapper.eq(CustomerGuestDO::getGuestLevel, guestLevel);
        if (businessStatus != -1)
            wrapper.eq(CustomerGuestDO::getBusinessStatus, businessStatus);
        if (lastBusinessDay != -1) {
            Date endDate = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endDate);
            calendar.add(Calendar.DAY_OF_MONTH, (-lastBusinessDay + 1));
            Date startDate = calendar.getTime();
            wrapper.ge(CustomerGuestDO::getLastBusinessDay, startDate)
                    .le(CustomerGuestDO::getLastBusinessDay, endDate);
        }

        //排序
        if (softOf != -1 && orderBy != -1) {
            if (orderBy == 1) {
                if (softOf == 1)
                    wrapper.orderByDesc(CustomerGuestDO::getGuestBenefits);
                if (softOf == 2)
                    wrapper.orderByDesc(CustomerGuestDO::getCreateTime);
//                if (softOf == 3)
//                    wrapper.orderByDesc(CustomerGuestDO::getCustomerBenefits);
            }
            else {
                if (softOf == 1)
                    wrapper.orderByAsc(CustomerGuestDO::getGuestBenefits);
                if (softOf == 2)
                    wrapper.orderByAsc(CustomerGuestDO::getCreateTime);
//                if (softOf == 3)
//                    wrapper.orderByDesc(CustomerGuestDO::getCustomerBenefits);
            }
        }
//        wrapper.orderByDesc(CustomerGuestDO::getIsPinTop);
        return wrapper;
    }

    private CustomerGuestDTO doToDto(CustomerGuestDO guestDO) {
        CustomerGuestDTO guestDTO = new CustomerGuestDTO();
        BeanUtils.copyProperties(guestDO, guestDTO);
        return guestDTO;
    }
}
